// Copyright 2011 Jefferson Willian
// willmont7@gmail.com

#include <cstdio>
#include <iostream>
#include <string>
#include <map>
#include <queue>

#define MAX 31

using std::map;
using std::string;
using std::cin;
using std::queue;

int numfriends[MAX];     // [i] -> Numero de amigos que 'i' exige
bool blocked[MAX][MAX];  // [i][j] -> 'i' exige que 'j' seja amigo
map<string, int> mymap;
queue<int> process;      // Fila de processos
queue<int> ret;          // Saida do programa

/* Resolve o problema */
bool solve(int n) {
  int curr;
  int count = 0;

  while (!process.empty()) {
    curr = process.front();
    ret.push(curr);
    count++;
    process.pop();
    for (int i = 0;i < n;i++)
      if (blocked[i][curr]) {
        if (--numfriends[i] == 0)
          process.push(i);
      }
  }

  /* Verifica se foi possivel add todos os amigos */
  if (count < n) {
    while (!ret.empty())
      ret.pop();
    return false;
  }

  return true;
}

int main() {
  int n, m;
  int t;
  string names[MAX];
  string name;
  int games = 1;

  while (scanf("%d", &n) && n != 0) {
    for (int i = 0;i < n;i++)
      for (int j = 0;j < n;j++)
        blocked[i][j] = false;

    for (int i = 0;i < n;i++) {
      cin >> names[i];
      mymap[names[i]] = i;
    }

    for (int i = 0;i < n;i++) {
      cin >> name;
      t = mymap[name];
      scanf("%d", &m);
      numfriends[t] = m;
      /* Condicao para entrar na fila de processos */
      if (m == 0)
        process.push(t);
      for (int j = 0;j < m;j++) {
        cin >> name;
        blocked[t][mymap[name]] = true;
      }
    }

    printf("Teste %d\n", games++);

    if (solve(n)) {
      while (!ret.empty()) {
        printf("%s ", names[ret.front()].c_str());
        ret.pop();
      }
    } else {
      printf("impossivel");
    }

    printf("\n\n");
  }

  return 0;
}
